package com.own.component.token.impl;

import com.own.component.common.exception.BusinessException;
import com.own.component.token.base.RequestTokenManager;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * DefaultMultipleRequestTokenManager
 *
 * @author chenxueli
 * @date 2023-11-07 18:47:36
 */
public class DefaultMultipleRequestTokenManager implements RequestTokenManager {

    private final List<RequestTokenManager> managerList = new ArrayList<>();

    public DefaultMultipleRequestTokenManager(RequestTokenManager... array) {
        // 筛选
        for (var manager : array) {
            if (manager.getClass() != DefaultMultipleRequestTokenManager.class) {
                managerList.add(manager);
            }
        }
        // 判断
        if (managerList.isEmpty()) {
            throw new BusinessException("没有可以使用的获取token的方法");
        }
    }

    /**
     * 获取token
     *
     * @param request 请求内容
     * @return 登录令牌信息
     */
    @Override
    public String token(HttpServletRequest request) {
        for (var manager : managerList) {
            var token = manager.token(request);
            if (StringUtils.isNotBlank(token)) {
                return token;
            }
        }
        return null;
    }

}
